<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/11
 * Time: 10:09
 */
namespace Redbag\Controller;
use Admin\Controller\CommonController;
use Trade\Model\TradeModel;
class RedbagController extends CommonController{
    private $trade_db;
    function _initialize(){
        parent::_initialize();
        $this->trade_db = new TradeModel();
    }

    /**
     * 红包用户统计
     */
    public function userList($page=1, $rows=10, $search = array()){
        if (IS_POST){
            $map=array();
            $limit = ($page - 1) * $rows . "," . $rows;

            $result = M('trade')->field('sum(score) as score,uid')->group('uid')->order('score desc')->limit($limit)->select();
            $total=count($result);
            $temp=array();
            foreach ($result as $k=>$v){
                $temp[$k]['nickname']=M('user')->where(array('uid'=>$v['uid']))->getField('nickname');
                $temp[$k]['headsmall']=M('user')->where(array('uid'=>$v['uid']))->getField('headsmall');

                $temp[$k]['give_score']=M('trade')->where(array('uid'=>$v['uid'],'type'=>array('in','1,3')))->sum('score');
                $temp[$k]['recieve_score']=M('trade')->where(array('uid'=>$v['uid'],'type'=>array('in','2,4')))->sum('score');
                $temp[$k]['tk_score']=M('trade')->where(array('uid'=>$v['uid'],'type'=>5))->sum('score');
                $temp[$k]['zr_score']=M('trade')->where(array('uid'=>$v['uid'],'type'=>6))->sum('score');
                $temp[$k]['zc_score']=M('trade')->where(array('uid'=>$v['uid'],'type'=>7))->sum('score');
                $temp[$k]['cz_score']=M('trade')->where(array('uid'=>$v['uid'],'type'=>8))->sum('score');
                $temp[$k]['tx_score']=M('trade')->where(array('uid'=>$v['uid'],'type'=>9))->sum('score');

            }


            $this->ajaxReturn(array('total'=>$total, 'rows'=>$temp));
        }else {
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('userList');
        }






//        if (IS_POST){
//            $map=array();
//            $limit = ($page - 1) * $rows . "," . $rows;
//            $total = $this->trade_db->where($map)->count();
//
//            $result=M('redbag')->field('sum(give_score) as give_score,uid')->group('uid')->order('give_score desc')->limit($limit)->select();
//            if($result){
//                foreach ($result as $k=>$v){
//                    $tempArr[$k]['give_score']=$v['give_score'];
//                    $tempArr[$k]['recieve_score']=M('redbag_detail')->where(array('uid'=>$v['uid']))->sum('score');
//                    $tempArr[$k]['nickname']=M('user')->where('uid='.$v['uid'])->getField('nickname');
//                    $tempArr[$k]['headsmall']=M('user')->where('uid='.$v['uid'])->getField('headsmall');
//                    $tempArr[$k]['uid']=$v['uid'];
//                }
//            }
//            $total=count($tempArr);
//            $this->ajaxReturn(array('total'=>$total, 'rows'=>$tempArr));
//        }else {
//            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
//            $this->display('userList');
//        }

    }

    /**
     * 红包用户详情
     */
    public function redbagUserDetail(){
        $result=M('trade')->where('uid='.I('uid'))->select();
        if($result){
            foreach ($result as $k=>$v){
                $tempArr[$k]['score']=$v['score'];
                $tempArr[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                if($v['type']==1){
                    $tempArr[$k]['type_text']='发红包';
                }else if($v['type']==2){
                    $tempArr[$k]['type_text']='抢红包';
                }else{
                    $tempArr[$k]['type_text']='其他';
                }
            }
        }
        $total=count($tempArr);
        $this->ajaxReturn(array('total'=>$total, 'rows'=>$tempArr));

        $this->assign('rows',$tempArr);
        $this->assign('total',$total);
        $this->display('redbagUserDetail');
    }
    /**
     * 红包充值
     */
    public function redbagAdd(){
        if(IS_POST){

        }else{
            $this->display('add');
        }
    }



    /**
     * 红包减值
     */
    public function redbagReduce(){
        echo 'redbagReduce';
    }
}